//
//  ZDCarButton.m
//  特种车调度
//
//  Created by 陈宇 on 15/8/20.
//
//

#import "ZDCarButton.h"

@interface ZDCarButton ()

@property (nonatomic, weak) UIView *indicateView;
@property (nonatomic, weak) UILabel *titleLabel;
@property (nonatomic, weak) UIImageView *imageView;

@end

@implementation ZDCarButton

- (id)initWithCoder:(NSCoder *)aDecoder
{
    if (self = [super initWithCoder:aDecoder]) {
        [self configuration];
    }
    return self;
}

- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        [self configuration];
    }
    return self;
}

- (void)configuration
{
    UIView *indicateView = [[UIView alloc] init];
    indicateView.backgroundColor = kGlobalColor;
    [self addSubview:indicateView];
    self.indicateView = indicateView;
    [indicateView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.right.mas_equalTo(self);
        make.height.mas_equalTo(2);
    }];
    
    UILabel *title = [[UILabel alloc] init];
    title.font = [UIFont systemFontOfSize:13.f];
    [self addSubview:title];
    self.titleLabel = title;
    [title mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self).offset(10.f);
        make.centerX.mas_equalTo(self);
    }];
    
    UIImageView *imageView = [[UIImageView alloc] init];
    [self addSubview:imageView];
    self.imageView = imageView;
    [imageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self);
        make.centerY.mas_equalTo(self).offset(10);
    }];
    
    [self setSelected:NO];
}

- (void)setSelected:(BOOL)selected
{
    _selected = selected;
    self.titleLabel.textColor = _selected ? kGlobalColor : [UIColor darkGrayColor];
    self.indicateView.hidden = !selected;
    self.imageView.image = _selected ? _selectImage : _image;
}

- (void)setImage:(UIImage *)image
{
    _image = image;
    self.imageView.image = image;
}

- (void)setTitle:(NSString *)title
{
    _title = [title copy];
    self.titleLabel.text = _title;
}

@end
